package com.guigu.common.util;

import com.guigu.common.mapper.GetUUidMapper;
import com.guigu.common.pojo.GetUUId;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Component
public class GetUUidUtil {
    @Resource
    public GetUUidMapper getUUidMapper;

    public String returnId(String id) {
        Date date=new Date();
        SimpleDateFormat sf=new SimpleDateFormat("yyyyMMdd");
        String format = sf.format(date);
        String s = id +format;
        List<GetUUId> list = this.getUUidMapper.queryUUid(id);
        if (list.size() > 0) {
            List<Long> list1 = new ArrayList<>();
            for (GetUUId l : list) {
                list1.add(Long.parseLong(l.getProduct_id().substring(l.getProduct_id().length()-4)));
            }
            Long max1 = list1.get(0);
            for (int i = 0; i < list1.size(); i++) {
                max1=max1>list1.get(i)?max1:list1.get(i);
            }
            String value="";
            if (id.equals("100")){
                value="100010101"+String.format("%06d",max1+1);
            }else {
                value=s+String.format("%04d",max1+1);
            }
            this.getUUidMapper.insertId(value);
            return value;
        } else {
            String value="";
            if (id.equals("100")){
                value="100010101000001";
            }else {
             value=s + "0001";
            }
            this.getUUidMapper.insertId(value);
            return value;
        }
    }
}
